package com.java.mybatis.controller;

import com.java.mybatis.entity.Area;
import com.java.mybatis.service.AreaService;
import com.java.mybatis.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Area)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/area/")
public class AreaController {
    /**
     * 服务对象
     */
    @Resource
    private AreaService areaService;

    @Resource
    private GoodsService goodsService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Area selectOne(Integer id) {
        return this.areaService.queryById(id);
    }

    /**
     * 查询所有货物地区的方法
     */
    @RequestMapping("getAllArea")
    public String getAllArea(Model model, Integer page, HttpSession session){
        try {
            List<Area> allArea = areaService.getAllArea((page-1)*7,7);
            for (Area area : allArea) {
                System.out.println(area);
            }
            model.addAttribute("area",allArea);
            //存入当前页码
            session.setAttribute("thisAreaPage",page);
            //获取总行数
            Integer areaCount = areaService.getAreaCount();
            model.addAttribute("count",areaCount);
            //获取总共有多少页
            int AreaPageMax= areaCount % 7 ==0 ? areaCount / 7 : areaCount / 7 + 1;
            model.addAttribute("AreaPageText","当前第"+page+"页,共"+AreaPageMax+"页");
            //存如总共多少页
            model.addAttribute("AreaPageMax",AreaPageMax);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/goods-list";
    }

    /**
     * 禁用货物地区修改状态的方法
     */
    @RequestMapping("upAreaCode")
    @ResponseBody
    public String upAreaCode(Integer id, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("UTF-8");
        try {
            Integer count = goodsService.selCount(id);
            if(count==0){
                areaService.upAreaCode(id);
            }else {
                return "该地区下还有货物未出库不能被禁用。";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
       return "禁用成功！";
    }

    /**
     * 启用货物地区修改状态的方法
     * @param id
     * @return
     */
    @RequestMapping("upAreaCodeup")
    @ResponseBody
    public String upAreaCodeup(Integer id,HttpSession session){
        try {
            Integer integer = areaService.selWareCode(id);
            if(integer==1){
                areaService.upAreaCodeup(id);
                return "启用成功";
            }else {
                return "请先启用大仓库！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer thisAreaPage = (Integer) session.getAttribute("thisAreaPage");
        return "redirect:/area/getAllArea?page="+thisAreaPage;
    }
    /**
     * 批量禁用的方法使用复选框
     */
    @RequestMapping("upAreaCodeAll")
    @ResponseBody
    public String upAreaCodeAll(String str,HttpSession session) throws Exception {
        String[] split = str.split(",");
        for (String s : split) {
            Integer count = goodsService.selCount(Integer.parseInt(s));
            if(count==0){
                areaService.upAreaCode(Integer.parseInt(s));
            }else {
                return "选择的地区中含有货物未出库，不能被禁用。";
            }
        }
        Integer thisAreaPage = (Integer) session.getAttribute("thisAreaPage");
       return "禁用成功！";
    }
}